package com.yaoweigroup.community.ui.my;

import org.codehaus.jackson.map.ObjectMapper;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.DisplayImageOptions.Builder;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.yaoweigroup.community.R;
import com.yaoweigroup.community.config.Constant;
import com.yaoweigroup.community.config.Urls;
import com.yaoweigroup.community.enity.UserInfo;
import com.yaoweigroup.community.ui.base.LoginActivity;
import com.yaoweigroup.community.ui.base.MyProfileActivity;
import com.yaoweigroup.community.util.LoginUtil;
import com.yaoweigroup.community.util.SharePreferenceUtil;
import com.yaoweigroup.community.util.Tools;
import com.yaoweigroup.community.util.Utils;

public class MyHomeActivity extends Activity implements OnClickListener {

	private ImageView icon_iv;
	private ImageView setting_iv;
	private RelativeLayout my_rl;
	private TextView name_tv;
	private RelativeLayout order_rl;
	private SharePreferenceUtil util;
	private TextView credit_tv;
	private TextView logout_tv;
	private RelativeLayout health_rl;
	private ImageView vifi_iv;

	private ObjectMapper mapper;

	private RelativeLayout discount_rl;
	private RelativeLayout wt_order_rl;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_my_home);

		mapper = new ObjectMapper();
		health_rl = (RelativeLayout) findViewById(R.id.health_rl);
		health_rl.setOnClickListener(this);
		util = new SharePreferenceUtil(this, Constant.prf);
		setting_iv = (ImageView) findViewById(R.id.setting_iv);
		setting_iv.setOnClickListener(this);
		credit_tv = (TextView) findViewById(R.id.credit_tv);
		icon_iv = (ImageView) findViewById(R.id.icon_iv);
		vifi_iv = (ImageView) findViewById(R.id.vifi_iv);

		logout_tv = (TextView) findViewById(R.id.logout_tv);
		logout_tv.setOnClickListener(this);
		Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
				R.drawable.test_icon);
		icon_iv.setImageBitmap(Utils.toRoundCorner(bitmap, 180));

		my_rl = (RelativeLayout) findViewById(R.id.my_rl);
		my_rl.setOnClickListener(this);

		name_tv = (TextView) findViewById(R.id.name_tv);
		name_tv.setOnClickListener(this);

		order_rl = (RelativeLayout) findViewById(R.id.order_rl);
		order_rl.setOnClickListener(this);
		icon_iv.setOnClickListener(this);

		discount_rl = (RelativeLayout) findViewById(R.id.discount_rl);
		discount_rl.setOnClickListener(this);

		wt_order_rl = (RelativeLayout) findViewById(R.id.wt_order_rl);
		wt_order_rl.setOnClickListener(this);
	}

	private UserInfo userInfo;

	@Override
	public void onResume() {
		super.onResume();
		LoginUtil.isLoginSuccess(this, handler, mapper);
		refresh();

	}

	private void refresh() {
		userInfo = (UserInfo) util.loadObject("user");
		if (userInfo != null) {
			String str = userInfo.getRealname();
			if (str == null || str.length() == 0) {
				str = userInfo.getUsername();
			}
			name_tv.setText(str);
			logout_tv.setVisibility(View.VISIBLE);
			displayHead(R.drawable.test_icon,
					Urls.imgUrl + userInfo.getIconPath(), icon_iv);
			credit_tv.setText("积分: " + splitDot(userInfo.getIntegral()));
			// displayHead(R.drawable.test_icon,
			// "http://www.smartcity.sh/Upload/20140610150154_7447.jpg",
			// icon_iv);
			vifi_iv.setVisibility(View.VISIBLE);
			if (userInfo.getIsign() == 0) {
				vifi_iv.setImageResource(R.drawable.no_id);
			} else {
				vifi_iv.setImageResource(R.drawable.yes_id);
			}

		} else {
			vifi_iv.setVisibility(View.GONE);
			name_tv.setText("点此登陆");
			logout_tv.setVisibility(View.GONE);
			Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
					R.drawable.test_icon);
			icon_iv.setImageBitmap(Utils.toRoundCorner(bitmap, 180));
			credit_tv.setText("0");
		}
	}

	/**
	 * 小数点处理
	 * 
	 * @param value
	 * @return
	 */
	public  String splitDot(double value) {

		try {
			String[] str2 = String.valueOf(value).split("\\.");
			if (str2.length > 1 && Integer.parseInt(str2[1]) > 0) {
				return value + "";
			} else {
				return str2[0] + "";
			}
		} catch (Exception e) {
			Log.e("wxf", value + "");
			return value + "";
		}

	}

	public void displayHead(int defaultIcon, String url, ImageView view) {
		Builder builder = new DisplayImageOptions.Builder();
		builder.displayer(new RoundedBitmapDisplayer(180));
		// builder.showImageOnFail(defaultIcon);
		// builder.showImageForEmptyUri(defaultIcon);
		builder.cacheInMemory(true);
		builder.cacheOnDisk(true);
		// builder.showImageOnLoading(defaultIcon);

		DisplayImageOptions displayImageOptions = builder.build();
		ImageLoader.getInstance().displayImage(url, view, displayImageOptions);
	}

	private Intent intent;

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.setting_iv:
			intent = new Intent(this, SettingActivity.class);
			startActivity(intent);
			break;
		case R.id.my_rl:
			if (userInfo == null) {
				Toast.makeText(this, "请登录以后 再操作!", 500).show();
				return;
			}
			intent = new Intent(this, MyProfileActivity.class);
			startActivity(intent);
			break;
		case R.id.name_tv:
			if (userInfo != null) {
				return;
			}
			intent = new Intent(this, LoginActivity.class);
			startActivity(intent);
			break;
		case R.id.order_rl:
			if (userInfo == null) {
				Toast.makeText(this, "请登录以后 再操作!", 500).show();
				return;
			}
			intent = new Intent(this, MyOrderActivity.class);
			startActivity(intent);
			break;
		case R.id.icon_iv:

			break;
		case R.id.logout_tv:
			util.removeKey("user");
			userInfo = null;
			refresh();
			break;
		case R.id.health_rl:
			if (userInfo == null) {
				Toast.makeText(this, "请登录以后 再操作!", 500).show();
				return;
			}
			intent = new Intent(this, MyHealthActivity.class);
			startActivity(intent);
			break;
		case R.id.discount_rl:
			if (userInfo == null) {
				Toast.makeText(this, "请登录以后 再操作!", 500).show();
				return;
			}
			intent = new Intent(this, MyDiscountActivity.class);
			startActivity(intent);
			break;
		case R.id.wt_order_rl:
			if (userInfo == null) {
				Toast.makeText(this, "请登录以后 再操作!", 500).show();
				return;
			}
			intent = new Intent(this, MyWTActivity.class);
			startActivity(intent);
			break;
		}
	}

	Handler handler = new Handler() {

		public void handleMessage(android.os.Message msg) {

			switch (msg.what) {
			case LoginUtil.LOGIN_FAIL:

				break;
			case LoginUtil.LOGIN_SUCCESS:
				refresh();
				break;
			}
		};
	};

	private long firstTime = 0;

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			long secondTime = System.currentTimeMillis();
			if (secondTime - firstTime > 2000) { // 如果两次按键时间间隔大于2秒，则不退出
				Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
				firstTime = secondTime;// 更新firstTime
				return true;
			} else { // 两次按键小于2秒时，退出应用
				System.exit(0);
			}
		}
		return super.onKeyDown(keyCode, event);
	}
}
